package demo1;

public class LeetCode202 {
    public boolean isHappy(int n) {
        int slow = n;
        int fast = bitSum(n);
        while(slow != fast) {
            slow = bitSum(slow);
            fast = bitSum(fast);
            fast = bitSum(fast);
        }

        return slow == 1;
    }

    private int bitSum(int n) {
        int sum = 0;
        while(n > 0) {
            int t =n % 10;
            sum += t * t;
            n /= 10;
        }
        return sum;
    }
}
